/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VisitaDialog.java
 *
 * Created on Jan 24, 2013, 5:12:15 PM
 */
package br.com.tbp.gui;

import br.com.tbp.controle.ControleConsultor;
import br.com.tbp.controle.ControleFunilVenda;
import br.com.tbp.exception.InputValidationException;
import br.com.tbp.exception.PersistenciaException;
import br.com.tbp.model.domain.Consultor;
import br.com.tbp.model.domain.Visita;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;

/**
 *
 * @author Diego
 */
public class VisitaDialog extends javax.swing.JDialog {

    private Visita visita;
    private Consultor consultor;
    private boolean novaVisita;
    private boolean readOnly;

    public VisitaDialog(java.awt.Frame parent, String title, boolean modal, Consultor consultor) {
        super(parent, title, modal);
        initComponents();
        setConsultor(consultor);
        this.setLocationRelativeTo(null);
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        idField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        razaoSocialField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        cnpjField = new javax.swing.JFormattedTextField();
        jLabel4 = new javax.swing.JLabel();
        responsavelField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        contatoField = new javax.swing.JFormattedTextField();
        jLabel6 = new javax.swing.JLabel();
        dataField = new org.jdesktop.swingx.JXDatePicker();
        jLabel7 = new javax.swing.JLabel();
        termometroCombo = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        linhasField = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        modensField = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        portabilidadeField = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        ofensorField = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        obsField = new javax.swing.JTextArea();
        jLabel16 = new javax.swing.JLabel();
        consultorField = new javax.swing.JTextField();
        cancelarButton = new javax.swing.JButton();
        salvarButton = new javax.swing.JButton();
        novaButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel17 = new javax.swing.JLabel();
        motivoCombo = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("ID visita:");

        idField.setEditable(false);
        idField.setText("0");
        idField.setFocusable(false);

        jLabel2.setText("Empresa:");

        jLabel3.setText("CNPJ:");

        try {
            cnpjField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.###.###/####-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        cnpjField.setFocusLostBehavior(javax.swing.JFormattedTextField.PERSIST);

        jLabel4.setText("Responsável:");

        jLabel5.setText("Contato:");

        try {
            contatoField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##) ####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        contatoField.setFocusLostBehavior(javax.swing.JFormattedTextField.PERSIST);

        jLabel6.setText("Data:");

        jLabel7.setText("Termômetro:");

        termometroCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0% Interessado", "25% Interessado", "50% Interessado", "75% Interessado", "100% Interessado" }));

        jLabel8.setText("Linhas:");

        linhasField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                linhasFieldFocusGained(evt);
            }
        });

        jLabel9.setText("Modens:");

        modensField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                modensFieldFocusGained(evt);
            }
        });

        jLabel13.setText("Portabilidade:");

        portabilidadeField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                portabilidadeFieldFocusGained(evt);
            }
        });

        jLabel14.setText("Principal ofensor:");

        jLabel15.setText("Observações:");

        obsField.setColumns(20);
        obsField.setRows(5);
        jScrollPane1.setViewportView(obsField);

        jLabel16.setText("Consultor:");

        consultorField.setEditable(false);
        consultorField.setFont(new java.awt.Font("Dialog", 1, 12));
        consultorField.setFocusable(false);

        cancelarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/delete.png"))); // NOI18N
        cancelarButton.setText("Cancelar");
        cancelarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarButtonActionPerformed(evt);
            }
        });

        salvarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/accept.png"))); // NOI18N
        salvarButton.setText("Salvar");
        salvarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salvarButtonActionPerformed(evt);
            }
        });

        novaButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/add16x16.png"))); // NOI18N
        novaButton.setText("Nova Visita");
        novaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                novaButtonActionPerformed(evt);
            }
        });

        jLabel17.setText("Motivo:");

        motivoCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nenhum", "Preço", "Cobertura", "Concorrência", "Sinal", "Falta de Assistência", "Outros" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(68, 68, 68)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel16)
                    .addComponent(jLabel8)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jLabel6)
                        .addComponent(jLabel3)
                        .addComponent(jLabel2)
                        .addComponent(jLabel1)
                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel5)
                        .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(idField, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(razaoSocialField, javax.swing.GroupLayout.DEFAULT_SIZE, 530, Short.MAX_VALUE)
                    .addComponent(responsavelField, javax.swing.GroupLayout.DEFAULT_SIZE, 530, Short.MAX_VALUE)
                    .addComponent(dataField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(termometroCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(linhasField, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(modensField, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(consultorField, javax.swing.GroupLayout.DEFAULT_SIZE, 530, Short.MAX_VALUE)
                    .addComponent(cnpjField, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(contatoField, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel14)
                    .addComponent(jLabel15)
                    .addComponent(jLabel13)
                    .addComponent(jLabel17))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 530, Short.MAX_VALUE)
                    .addComponent(portabilidadeField, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ofensorField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 530, Short.MAX_VALUE)
                    .addComponent(motivoCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(novaButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 356, Short.MAX_VALUE)
                .addComponent(salvarButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cancelarButton)
                .addContainerGap())
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 698, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(idField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel16)
                    .addComponent(consultorField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(dataField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(razaoSocialField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cnpjField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(responsavelField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(contatoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(termometroCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(linhasField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(modensField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel13)
                        .addGap(9, 9, 9)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel14)
                            .addComponent(ofensorField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(9, 9, 9)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel17)
                            .addComponent(motivoCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(portabilidadeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel15)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelarButton)
                    .addComponent(salvarButton)
                    .addComponent(novaButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarButtonActionPerformed
        dispose();
    }//GEN-LAST:event_cancelarButtonActionPerformed

    private void salvarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salvarButtonActionPerformed
        try {
            this.validarEntradaDoUsuario();

            ControleFunilVenda controle = new ControleFunilVenda();
            Visita visita = this.getVisita();
            if (isNovaVisita()) {
                controle.registrarVisita(visita);
                JOptionPane.showMessageDialog(null, "A visita foi salva com sucesso", "Mensagem do sistema", 1);
            } else {
                controle.atualizarVisita(visita);
                JOptionPane.showMessageDialog(null, "A visita foi atualizada com sucesso", "Mensagem do sistema", 1);
            }
        } catch (InputValidationException ivex) {
            JOptionPane.showMessageDialog(null, ivex.getMessage(), "Mensagem do sistema", 0);
            System.out.println(ivex.getMessage());
        } catch (PersistenciaException pex) {
            System.out.println(pex.getMessage());
        }
    }//GEN-LAST:event_salvarButtonActionPerformed

    private void novaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_novaButtonActionPerformed
        cleanVisita();
    }//GEN-LAST:event_novaButtonActionPerformed

    private void linhasFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_linhasFieldFocusGained
        linhasField.selectAll();
    }//GEN-LAST:event_linhasFieldFocusGained

    private void modensFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_modensFieldFocusGained
        modensField.selectAll();
    }//GEN-LAST:event_modensFieldFocusGained

    private void portabilidadeFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_portabilidadeFieldFocusGained
        portabilidadeField.selectAll();
    }//GEN-LAST:event_portabilidadeFieldFocusGained

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                VisitaDialog dialog = new VisitaDialog(new javax.swing.JFrame(), "Visita - Funil de Vendas", true, null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelarButton;
    private javax.swing.JFormattedTextField cnpjField;
    private javax.swing.JTextField consultorField;
    private javax.swing.JFormattedTextField contatoField;
    private org.jdesktop.swingx.JXDatePicker dataField;
    private javax.swing.JTextField idField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField linhasField;
    private javax.swing.JTextField modensField;
    private javax.swing.JComboBox motivoCombo;
    private javax.swing.JButton novaButton;
    private javax.swing.JTextArea obsField;
    private javax.swing.JTextField ofensorField;
    private javax.swing.JTextField portabilidadeField;
    private javax.swing.JTextField razaoSocialField;
    private javax.swing.JTextField responsavelField;
    private javax.swing.JButton salvarButton;
    private javax.swing.JComboBox termometroCombo;
    // End of variables declaration//GEN-END:variables

    @Override
    protected JRootPane createRootPane() {
        ActionListener closeListener = new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                cancelarButtonActionPerformed(e);
            }
        };
        KeyStroke stroke =
                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        JRootPane myRootPane = new JRootPane();
        myRootPane.registerKeyboardAction(
                closeListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);

        return myRootPane;
    }

    private void setConsultor(Consultor consultor) {
        if (consultor == null) {
            return;
        }
        this.consultor = consultor;
        try {
            consultorField.setText(new ControleConsultor().pesquisar(consultor.getId()).getNome());
        } catch (PersistenciaException pex) {
            System.out.println(pex.getMessage());
        }
    }

    public Consultor getConsultor() {
        return consultor;
    }

    public void validarEntradaDoUsuario() throws InputValidationException {
        if (dataField.getDate() == null) {
            dataField.requestFocus();
            throw new InputValidationException("A data da visita deve ser informada");
        }
        if (razaoSocialField.getText().isEmpty()) {
            razaoSocialField.requestFocus();
            throw new InputValidationException("A empresa deve ser informada");
        }
    }

    public boolean isNovaVisita() {
        return novaVisita;
    }

    public void setNovaVisita(boolean novaVisita) {
        this.novaVisita = novaVisita;
    }

    public Visita getVisita() {
        if (visita == null) {
            visita = new Visita();
        }
        visita.setData(dataField.getDate());
        visita.setRazaoSocial(razaoSocialField.getText().toUpperCase().trim());
        visita.setCnpj(cnpjField.getText());
        visita.setResponsavel(responsavelField.getText().toUpperCase().trim());
        visita.setContato(contatoField.getText());
        visita.setTermometro(termometroCombo.getSelectedItem().toString());
        visita.setQtdeLinhas(linhasField.getText());
        visita.setQtdeModens(modensField.getText());
        visita.setPortabilidade(portabilidadeField.getText());
        visita.setPrincipalOfensor(ofensorField.getText().toUpperCase());
        visita.setMotivo(motivoCombo.getSelectedItem().toString());
        visita.setObs(obsField.getText().trim());
        visita.setConsultor(getConsultor());
        return visita;
    }

    public void setVisita(Visita visita) {
        this.visita = visita;
        idField.setText(String.valueOf(visita.getId()));
        //consultorField.setText(visita.getConsultor().getNome());
        dataField.setDate(visita.getData());
        razaoSocialField.setText(visita.getRazaoSocial());
        cnpjField.setText(visita.getCnpj());
        responsavelField.setText(visita.getResponsavel());
        contatoField.setText(visita.getContato());
        termometroCombo.setSelectedItem(visita.getTermometro());
        linhasField.setText(visita.getQtdeLinhas());
        modensField.setText(visita.getQtdeModens());
        portabilidadeField.setText(visita.getPortabilidade());
        ofensorField.setText(visita.getPrincipalOfensor());
        motivoCombo.setSelectedItem(visita.getMotivo());
        obsField.setText(visita.getObs());
        obsField.setCaretPosition(0);
    }

    private void cleanVisita() {
        this.visita = null;
        this.novaVisita = true;
        idField.setText(null);
        dataField.setDate(null);
        razaoSocialField.setText(null);
        cnpjField.setText(null);
        responsavelField.setText(null);
        contatoField.setText(null);
        termometroCombo.setSelectedIndex(0);
        linhasField.setText(null);
        modensField.setText(null);
        motivoCombo.setSelectedIndex(0);
        portabilidadeField.setText(null);
        ofensorField.setText(null);
        obsField.setText(null);
        obsField.setCaretPosition(0);
        dataField.requestFocus();
    }

    public boolean isReadOnly() {
        return readOnly;
    }

    public void setReadOnly(boolean readOnly) {
        this.readOnly = readOnly;
        if (isReadOnly()) {
            novaButton.setVisible(false);
            salvarButton.setEnabled(false);
        }
    }
}
